//
//  HJPlayerVC.m
//  project
//
//  Created by yefei on 2019/7/20.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJPlayerVC.h"
#import "HJVideoView.h"
#import "GKBallLoadingView.h"
#import "HJViewModel.h"
#import "HJCommentView.h"
#import "GKSlidePopupView.h"
#import "HJInfoDetailVC.h" // 个人中心
#import "HJVideoModel.h"
#import "HJShareView.h" // 分享
#import "YFJourneyModel.h"
#import "HJUpdateVC.h"

@interface HJPlayerVC ()<HJVideoViewDelegate>
@property (nonatomic, strong) HJVideoView *videoView;
/**
 检查版本跟新
 */
@property (nonatomic,strong) HJUpdateVC * updateVC;
/**
 返回
 */
@property (nonatomic,strong) UIButton * backItem;
@end

#define kTitleViewY         (GK_SAVEAREA_TOP + 20.0f)
// 过渡中心点
#define kTransitionCenter   20.0f
// 适配比例
#define ADAPTATIONRATIO     SCREEN_WIDTH / 750.0f

@implementation HJPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.extendedLayoutIncludesOpaqueBars = YES;
    // 更新
    [self loadUpdateCheckVersion];
    
    [self.view addSubview:self.videoView];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    if (!self.isPush) {
        // 模拟加载
        GKBallLoadingView *loadingView = [GKBallLoadingView loadingViewInView:self.view];
        [loadingView startLoading];
        @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [loadingView stopLoading];
            [loadingView removeFromSuperview];
        });
        [self.videoView.viewModel refreshNewListWithSuccess:^(NSArray * _Nonnull list) {
            @strongify(self);
            [self.videoView setModels:list index:0];
        } failure:^(NSString * _Nonnull error) {
            [YFHud showError:error];
        }];
    }else {
        [self.view addSubview:self.backItem];
        [self.backItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@20);
            make.top.equalTo(@34);
            make.size.mas_equalTo(CGSizeMake(31, 31));
        }];
        [self.videoView setModels:self.dataArray index:self.indexPath.row];
    }
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.videoView resume];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 停止播放
    [self.videoView pause];
    [self.navigationController setNavigationBarHidden:NO];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)dealloc {
    [self.videoView destoryPlayer];
}

#pragma mark -- 加载UI
-(HJVideoView *)videoView {
    if (!_videoView) {
        _videoView = [[HJVideoView alloc] initWithVC:self isPushed:self.isPush];
        _videoView.delegate = self;
    }
    return _videoView;
}
-(UIButton *)backItem {
    if (!_backItem) {
        _backItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backItem setImage:[UIImage imageNamed:@"back_black_left"] forState:0];
        _backItem.adjustsImageWhenHighlighted = YES;
        @weakify(self)
        [[_backItem rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    return _backItem;
}
#pragma mark -- 评论
- (void)videoView:(nonnull HJVideoView *)videoView didClickComment:(nonnull HJVideoModel *)videoModel {
    HJCommentView *commentView = [[HJCommentView alloc]init];
    commentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH/4*3);
    GKSlidePopupView *popupView = [GKSlidePopupView popupViewWithFrame:UIScreen.mainScreen.bounds contentView:commentView];
    [popupView showFrom:[UIApplication sharedApplication].keyWindow completion:^{
        [commentView loadDataWorkWithModel:videoModel];
    }];
    commentView.close = ^{
        [popupView dismiss];
    };
}
#pragma mark -- 头像
- (void)videoView:(nonnull HJVideoView *)videoView didClickIcon:(nonnull HJVideoModel *)videoModel {
    HJInfoDetailVC * infoDetailVC = [HJInfoDetailVC new];
    infoDetailVC.userModel = videoModel.user;
    infoDetailVC.userModel.concern = videoModel.concern;
    [self.navigationController pushViewController:infoDetailVC animated:YES];
}
#pragma mark -- 分享
- (void)videoView:(nonnull HJVideoView *)videoView didClickShare:(nonnull HJVideoModel *)videoModel {
    NSString *dynUrl;
    if (videoModel.isVideo) {
        dynUrl = [NSString stringWithFormat:@"http://www.lejianyou.com/share.html?fileType=video&fileUrl=%@", videoModel.videoSource.url];
    } else {
        if (videoModel.imagesSource.count > 0) {
            dynUrl = [NSString stringWithFormat:@"http://www.lejianyou.com/share.html?fileType=image&fileUrl=%@", videoModel.imagesSource[0]];
        }
    }
    HJShareView * shareView = [HJShareView shareView];
    shareView.shareUrl = dynUrl;
    [shareView show];
}

- (void)videoView:(nonnull HJVideoView *)videoView didPanWithDistance:(CGFloat)distance isEnd:(BOOL)isEnd { 
    
}

- (void)videoView:(nonnull HJVideoView *)videoView didScrollIsCritical:(BOOL)isCritical { 
    
}


#pragma mark - 弹起更新弹窗
-(HJUpdateVC *)updateVC {
    if (!_updateVC) {
        _updateVC = [[HJUpdateVC alloc]init];
        _updateVC.view.frame = CGRectMake(0, SCREEN_HEIGTH, SCREEN_WIDTH, SCREEN_HEIGTH);
        [[UIApplication sharedApplication].keyWindow addSubview:_updateVC.view];
    }
    return _updateVC;
}

-(void)loadUpdateCheckVersion {
    WS(weakSelf)
    NSString * url = [Api stringByAppendingString:checkVersionApi];
    [HJHttpTool post:url params:@{@"deviceType":@"iphone"} success:^(HJResponeds *result) {
        if (![result.data isEqual:[NSNull null]] && [result.data[@"status"] integerValue] == 1 ) {
            [weakSelf.updateVC updateLoadDataWithDic:result.data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.5 animations:^{
                    weakSelf.updateVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH);
                } completion:^(BOOL finished) {
                    weakSelf.updateVC = nil;
                }];
            });
        }
    } failure:^(id result) {
    }];
}

@end
